package com.graduation.fq.teacher.ui.activity;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.graduation.fq.teacher.R;
import com.graduation.fq.teacher.interfaces.EventListener;
import com.graduation.fq.teacher.logic.InformationLogic;
import com.graduation.fq.teacher.module.CourseInfo;
import com.graduation.fq.teacher.module.EventArgs;
import com.graduation.fq.teacher.utils.Const;
import com.graduation.fq.teacher.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jifang
 * @version 2016/5/22.
 */
public class InformationActivity extends Activity {
    private int flag;
    private LinearLayout editContainer;
    private List<View> viewList = new ArrayList<>();
    private InformationLogic logic;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_information);
        logic = InformationLogic.getInstance();
        flag = getIntent().getIntExtra(Const.ACTIVITY_FLAG, -1);
        TextView informationText = (TextView) findViewById(R.id.information_title);
        editContainer = (LinearLayout) findViewById(R.id.edit_container);
        Button sureBt = (Button) findViewById(R.id.information_sure);
        Button moreBt = (Button) findViewById(R.id.information_more);
        initTitle(flag, informationText);
        initEditContainer(flag);
        sureBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                createCourseInfo();
            }
        });
        moreBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addEditContainer();
            }
        });
    }

    private void createCourseInfo() {
        CourseInfo info;
        View view = viewList.get(0);
        EditText nameEdit = (EditText) view.findViewById(R.id.course_information_name);
        EditText creditEdit = (EditText) view.findViewById(R.id.course_information_credit);

        info = new CourseInfo();
        info.setName(nameEdit.getText().toString());
        info.setCredit(Integer.parseInt(creditEdit.getText().toString()));
        info.setTeacherId(getUserId());
        logic.addCourseInfo(info, new EventListener() {
            @Override
            public void onEvent(EventArgs args) {
                int status = args.getStatus();
                if (status == Const.HTTP_SUCCESS) {
                    ToastUtils.Toast(InformationActivity.this, "添加成功");
                } else {
                    ToastUtils.Toast(InformationActivity.this, "添加失败");
                }
            }
        });
    }

    private String getUserId() {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
        String id = preferences.getString("user_id", "");
        return id;
    }

    private void addEditContainer() {
        View view = getLayoutInflater().inflate(R.layout.layout_information_course_item, null);
        viewList.add(view);
        editContainer.addView(view);
    }

    /**
     * 初始化输入布局
     *
     * @param flag
     */
    private void initEditContainer(int flag) {
        View view = getLayoutInflater().inflate(R.layout.layout_information_course_item, null);
        viewList.add(view);
        editContainer.addView(view);
    }

    /**
     * 设置页面标题
     *
     * @param flag
     * @param textView
     */
    private void initTitle(int flag, TextView textView) {
        if (flag == Const.ACTIVITY_FLAG_CLASS) {
            textView.setText("录入班级信息");
        } else if (flag == Const.ACTIVITY_FLAG_COURSE) {
            textView.setText("录入课程信息");
        } else {
            textView.setText("录入学生信息");
        }
    }
}
